package com.example.myabtisdemo.controller;

import com.example.myabtisdemo.dao.TemperatureDao;
import com.example.myabtisdemo.entity.Temperature;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author chaird
 * @create 2021-12-04 21:08
 */
@RestController
public class TempController {

  @Autowired private TemperatureDao temperatureDao;

  @GetMapping("/hello")
  public Object doSomeThing() {

    List<Temperature> temperatures = temperatureDao.selectByExample(null);

    int size = temperatures.size();

    for (int i = 1000; i < size; i++) {

      Temperature temperature = temperatures.get(i);

      if ((Math.random() * 100) < 5) {
        temperature.setValue(temperature.getValue() + 10);
        temperature.setMemo(0);
        temperatureDao.updateByPrimaryKey(temperature);
      }
    }

    return temperatures;
  }

  @GetMapping("/page")
  public Object page() {
    // 查询第三页，每页三条
    PageHelper.startPage(3, 3);
    List<Temperature> temperatures = temperatureDao.selectByExample(null);
    //com.github.pagehelper.Page
    System.out.println(temperatures.getClass().getName());
    // 得到分页的结果对象
    PageInfo<Temperature> resPage = new PageInfo<>(temperatures);
    return resPage;
  }
}
